package com.gitee.feizns.quickstart.ding.talk.event.contacts;

import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import lombok.RequiredArgsConstructor;
import shade.com.alibaba.fastjson2.JSONObject;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * 用户修改组织侦听器
 * @author feizns
 * @since 2025/3/24
 */
@RequiredArgsConstructor
public class UserModifyOrgListener implements GenericEventListener {

    /**
     * 处理
     */
    private final BiConsumer<String, JSONObject> handler;

    @Override
    public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
        return onEvent(event, handler);
    }

    /**
     * 活动
     * @param event 事件
     * @param handler 处理器
     * @return {@link EventAckStatus }
     */
    public static EventAckStatus onEvent(GenericOpenDingTalkEvent event,
                                         BiConsumer<String, JSONObject> handler) {
        if ( "user_modify_org".equals(event.getEventType()) ) {
            List<String> userId = event.getData().getList("userId", String.class);
            if ( handler != null )
                userId.forEach(userid -> handler.accept(userid, event.getData()));
        }
        return EventAckStatus.SUCCESS;
    }

}
